package main
// https://www.jianshu.com/p/24a55775e266
// https://www.jianshu.com/p/c308f5953d60 xxxxxxxxxxxxxxxx
// https://blog.csdn.net/maxdaic/category_10073273.html
// https://www.jianshu.com/p/6745c5561ba4
// https://blog.csdn.net/ahy231/article/details/114112638
import (
	"context"
	"fmt"
	"github.com/ethereum/go-ethereum"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/ethclient"
	"log"
)

func main() {
	client, err := ethclient.Dial("ws://127.0.0.1:8545")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("we have a connection")
	_ = client // we'll use this in the upcoming sections
	contractAddress := common.HexToAddress("0x2186d89AB52278bEA98c3B2Ffe10d3A184cad788")

	query := ethereum.FilterQuery{
		Addresses: []common.Address{contractAddress},
	}
	logs := make(chan types.Log)
	sub, err := client.SubscribeFilterLogs(context.Background(), query, logs)
	if err != nil {
		fmt.Printf("err: %v\n", err.Error())
	}
	for {
		select {
		case err :=<- sub.Err():
			log.Fatal(err.Error())
		case vLog := <-logs:
			fmt.Printf("vLog: %v\n", vLog)
		}
	}
}
